<?php
/**
 * Created by qinpeizhou.
 * Date: 2018/3/24
 * Time: 22:57
 * Email : 1031219129@qq.com
 */

include_once( dirname(__FILE__).'/../library/exception/SignFailException.php' );

class IAuth{

    //设置密码
    public static function setPassword($data){
        $salt = parse_ini_file(dirname(__FILE__).'/../extra/secure.php');
        return md5($data.$salt['pasword_pre_halt']);
    }


    /**
     * 生成每次请求的的sign
     * @param array $data
     * @return HexString|string
     */
    public static function setSign($data = []){
           // 1.按字段排序
           ksort($data);
           // 2.拼接字符串
           $string = http_build_query($data);
           // 3.通过aes来加密
           $string = (new Aes())->encrypt($string);
           echo $string;die;
           return $string;
    }

    /**
     * 检查sign是否正常
     * @param $data
     * @return bool
     * @throws SignFailException
     */
    public static function checkSignPass($data){
          try{
              $str = (new Aes())->decrypt($data['sign']);

              if(empty($str)){
                  return false;
              }
              //抽取 did=xxxx&app_type=3成数组
              parse_str($str,$arr);
//            if(!is_array($arr) || empty($arr['did'] || $arr['did'] != $data['did'])){
//               return false;
//            }
              return true;
          }catch (Exception $e){
              throw new SignFailException();
          }

    }
    
}
